import { createRouter, createWebHistory } from 'vue-router'

// 统一登录页面
const Login = () => import('../views/Login.vue')

// 管理端页面
const AdminDashboard = () => import('../views/admin/Dashboard.vue')

// 学生端页面（使用统一登录页面）

// 占位组件
const Placeholder = () => import('../views/Placeholder.vue')

const routes = [
  {
    path: '/',
    redirect: '/admin/login'
  },
  {
    path: '/login',
    redirect: '/admin/login'
  },
  // 管理端路由
  {
    path: '/admin/login',
    name: 'AdminLogin',
    component: Login,
    meta: { title: '管理员登录' }
  },
  {
    path: '/admin',
    component: () => import('../views/admin/AdminLayout.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        redirect: 'dashboard'
      },
      {
        path: 'dashboard',
        name: 'AdminDashboard',
        component: AdminDashboard,
        meta: { title: '管理首页', requiresAuth: true }
      },
      {
        path: 'students',
        name: 'AdminStudents',
        component: () => import('../views/admin/Students.vue'),
        meta: { title: '学生管理', requiresAuth: true }
      },
      {
        path: 'majors',
        name: 'AdminMajors',
        component: () => import('../views/admin/Majors.vue'),
        meta: { title: '专业管理', requiresAuth: true }
      },
      {
        path: 'classes',
        name: 'AdminClasses',
        component: () => import('../views/admin/Classes.vue'),
        meta: { title: '班级管理', requiresAuth: true }
      },
      {
        path: 'dormitory',
        name: 'AdminDormitory',
        component: () => import('../views/admin/Dormitories.vue'),
        meta: { title: '宿舍管理', requiresAuth: true }
      },
      {
        path: 'registration',
        name: 'AdminRegistration',
        component: () => import('../views/admin/Registration.vue'),
        meta: { title: '报到管理', requiresAuth: true }
      },
      {
        path: 'finance',
        name: 'AdminFinance',
        component: () => import('../views/admin/Finance.vue'),
        meta: { title: '财务管理', requiresAuth: true }
      },
      {
        path: 'payment-records',
        name: 'AdminPaymentRecords',
        component: () => import('../views/admin/PaymentRecords.vue'),
        meta: { title: '缴费记录', requiresAuth: true, role: 'admin' }
      },
      {
        path: 'users',
        name: 'AdminUsers',
        component: () => import('../views/admin/Users.vue'),
        meta: { title: '用户管理', requiresAuth: true }
      },
      {
        path: 'notices',
        name: 'AdminNotices',
        component: () => import('../views/admin/Notices.vue'),
        meta: { title: '通知公告', requiresAuth: true }
      }
    ]
  },
  // 学生端路由
  {
    path: '/student/login',
    name: 'StudentLogin',
    component: Login,
    meta: { title: '学生登录' }
  },
  {
    path: '/student',
    component: () => import('../views/student/StudentLayout.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        redirect: 'home'
      },
      {
          path: 'home',
          name: 'StudentHome',
          component: () => import('../views/student/Home.vue'),
          meta: { title: '学生首页', requiresAuth: true }
        },
        {
          path: 'profile',
          name: 'StudentProfile',
          component: () => import('../views/student/Profile.vue'),
          meta: { title: '个人信息', requiresAuth: true }
        },
        {
          path: 'registration',
          name: 'StudentRegistration',
          component: () => import('../views/student/Registration.vue'),
          meta: { title: '报到办理', requiresAuth: true }
        },
        {
          path: 'dormitory',
          name: 'StudentDormitory',
          component: () => import('../views/student/Dormitory.vue'),
          meta: { title: '宿舍申请', requiresAuth: true }
        },
        {
          path: 'payment',
          name: 'StudentPayment',
          component: () => import('../views/student/Payment.vue'),
          meta: { title: '缴费中心', requiresAuth: true }
        },
        {
          path: 'schedule',
          name: 'StudentSchedule',
          component: () => import('../views/student/Schedule.vue'),
          meta: { title: '报到流程', requiresAuth: true }
        },
        {
          path: 'notices',
          name: 'StudentNotices',
          component: () => import('../views/student/Notices.vue'),
          meta: { title: '通知公告', requiresAuth: true }
        },
        {
          path: 'help',
          name: 'StudentHelp',
          component: () => import('../views/student/Help.vue'),
          meta: { title: '帮助中心', requiresAuth: true }
        },
        // 保留原有页面的兼容性路由
        {
          path: 'info-submit',
          name: 'StudentInfoSubmit',
          component: () => import('../views/student/InfoSubmit.vue'),
          meta: { title: '信息提交', requiresAuth: true }
        },
        {
          path: 'info-query',
          name: 'StudentInfoQuery',
          component: () => import('../views/student/InfoQuery.vue'),
          meta: { title: '信息查询', requiresAuth: true }
        },
        {
          path: 'contact',
          name: 'StudentContact',
          component: () => import('../views/student/Contact.vue'),
          meta: { title: '联系我们', requiresAuth: true }
        },
        {
          path: 'dormitory-application',
          name: 'StudentDormitoryApplication',
          component: () => import('../views/student/DormitoryApplication.vue'),
          meta: { title: '宿舍申请', requiresAuth: true, role: 'student' }
        }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫（暂时注释掉，方便预览）
// router.beforeEach((to, from, next) => {
//   if (to.meta.requiresAuth) {
//     // 检查登录状态
//     const isAuthenticated = localStorage.getItem('token')
//     if (!isAuthenticated) {
//       next({ name: to.path.includes('admin') ? 'AdminLogin' : 'StudentLogin' })
//       return
//     }
//   }
//   next()
// })

export default router